package com.gulimal.lsearch.controller;

import com.gulimal.lsearch.service.ProductSaveService;
import com.gulimall.common.exception.BizCodeEnume;
import com.gulimall.common.to.es.SkuEsModel;
import com.gulimall.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@Slf4j
@RequestMapping("/search")
@RestController
public class ElasticsearchController {

    @Autowired
    ProductSaveService productSaveService;

    @PostMapping("/save/product")
    public R productStatusUp(@RequestBody List<SkuEsModel> skuEsModels){
        boolean b = true;
        try {
            b =  productSaveService.productStatusUp(skuEsModels);
        } catch (IOException e) {
            log.error("ES商品上架错误：{}",e.getMessage());
            return R.error(BizCodeEnume.PRODUCT_UP_EXCEPTION.getCode(),BizCodeEnume.PRODUCT_UP_EXCEPTION.getMessage());
        }
        return !b == true ? R.ok() : R.error(BizCodeEnume.PRODUCT_UP_EXCEPTION.getCode(),BizCodeEnume.PRODUCT_UP_EXCEPTION.getMessage());
    }
}
